var config = { //初始化websql数据库的参数信息
	name: 'JLDB',
	version: '1.0',
	desc: 'jiulong DB',
	size: 20 * 1024
};
var db = window.openDatabase(config.name, config.version, config.desc, config.size);
function errorCallback(error) {
	$.each(error, function(i,k) {
		console.info(i);
		console.info(k);
	});
	
}
var TBS = {
	users : ['ID','PWD','UNAME','USERS1','USERS2'],
	sf : ['BD_NO','ITEM','BITEM','ZWZFL','ZLYFS','ZJNUM','ZWZBY','ZSFID','ZCLJG','ZFJBZ'],
	zw : ['BD_NO','ITEM','BITEM','ZWZFL','ZLYFS','ZJNUM','ZWZBY','ZCLJG','ZJDBZ','ZJSBL','ZFJBZ','BZTX'],
	zz : ['BD_NO','ITEM','BITEM','ZWZFL','ZLYFS','ZJNUM','ZWZBY','ZCLJG','ZJDBZ','ZJSBL','ZFJBZ','BZTXT'],
	conf : ['KEY','VAL']
};
function create(table,fields){
	var sql = 'create table if not exists '+table+'('+fields.join(',')+')';
	console.log(sql);
	db.transaction(function(tx) {
		tx.executeSql(sql, null, function(tx, rs) {
			console.log('执行sql成功');
		}, errorCallback);
	});
}
function insert(table,fields,row,fn){
	var sql = 'INSERT INTO '+table+' ('+fields.join(',')+') VALUES ('+getWH(fields.length)+')';
	db.transaction(function(tx) {
		tx.executeSql(sql, row, function(tx, rs) {
			console.log('Insert' + JSON.stringify(row)); 
			if(fn){ fn(row); }
		}, errorCallback);
	});
}

function update(table,dat,where,fn){
	var fk = [];
	var values = [];
	$.each(dat, function(k,v) {
		fk.push(k+'=?');
		values.push(v);
	});
	var sql = 'UPDATE '+table+' SET '+fk.join(',');
	var whe = [];
	$.each(where, function(k,v) {
		whe.push(k+'=?');
		values.push(v);
	});
	if(whe.length>0){
		sql += ' Where '+ whe.join(','); 
	}
	console.log(sql);
	console.log(values.join(','));
	db.transaction(function(tx) {
		tx.executeSql(sql, values, function(tx, rs) {
			console.log('Update：' + JSON.stringify(row)); 
			if(fn)fn(rs.rowsAffected);
		}, errorCallback);
	});
}

function getWH(len){
	var arr = [];
	while(len>0){ arr.push('?'); len--; }
	return arr;
}

function select(table,fields,conf,fn){ 
	var sql = 'SELECT '+fields.join(',')+' FROM '+table;
	var values = [];
	if(conf.where){
		var fk = [];
		$.each(conf.where, function(k,v) {
			fk.push(k+'=?');
			values.push(v);
		});
		sql +=' WHERE '+fk.join(' and ');
	}
	if(conf.order){
		var fk = [];
		$.each(conf.order, function(k,v) {
			fk.push(k+'=?');
			values.push(v);
		});
		sql +=' ORDER BY '+fk.join(',');
	} 
	if(conf.limit){
		sql +=' LIMIT = ? ';
		values.push(conf.limit);
	}
	db.transaction(function(tx) {
		tx.executeSql(sql, values, function(tx, rs) {
			if(fn){ fn(rs.rows);  };
		}, errorCallback);
	});
}
function remove(table,conf,fn){ 
	var sql = 'DELETE FROM '+table;
	var fk = [];
	var values = [];
	if(conf.where){
		$.each(conf.where, function(k,v) {
			fk.push(k+'=?');
			values.push(v);
		});
		sql += ' WHERE '+fk.join(',');
	}
	console.log(sql);
	db.transaction(function(tx) {
		tx.executeSql(sql, values, function(tx, rs) {
			if(fn){ fn(rs)};
		}, errorCallback);
	}); 
}

function drop(table){
	var sql = 'DROP TABLE '+table  ; 
	db.transaction(function(tx) {
		tx.executeSql(sql, null, function(tx, rs) {
			console.log('执行sql成功');
		}, errorCallback);
	});
	
}
function DropTables(){
	$.each(TBS, function(i,d) {
		drop(i);
	});
}
function InitTables(){
	$.each(TBS, function(i,d) {
		create(i,d);
	});
}
function getSession(id){
	return localStorage.getItem(id);
}
function setSession(id,val){
	return localStorage.setItem(id,val);
}
/*
InitTables();
insert('users',TBS.users,['admin','admin','管理','G','0']); 
*/